/*客户端登录模块*/
#include "head.h"

/*登录选项*/
int menu(int sockfd)
{
    int i = 0;
    //people peo;
    printf("----------请输入选项----------\n");
    printf("\t  1.用户登录\n");
    printf("\t  2.用户注册\n");
    printf("\t  3.管理员登录\n");
    //printf("\t  4.添加管理员\n");
    printf("\t  0.退出\n");
    printf("-----------------------------\n");

    while(1)
    {
        scanf("%d", &i);
        if(i<4 && i>=0)
            break;
        else 
            printf("请重新输入\n");
    }
    switch (i)
    {
    case 1:
        i = sign_up(sockfd,1);
        break;
    case 2:
        i = sign_in(sockfd,1);
        break;
    case 3:
        i = sign_up(sockfd,0);       //管理员登录
        break;
    case 0:
        printf("退出程序\n");
        return 7;
    default:
        printf("错误选项\n");
        break;
    }
    return i;
}

/*登录模块*/
int sign_up(int sockfd,int n)
{
    requst buf_3 = {2};
    char buf_5[15] = {0};
    char buf_6[12] = {0};
    if(n==0)
    {
        buf_3.who=1;
    }
    if(n==1)
    {
        buf_3.who=0;
    }

    printf("请输入ID\n");
    scanf("%d", &buf_3.id);
    printf("请输入密码\n");
    scanf("%s", buf_3.pass);

    send(sockfd, &buf_3, sizeof(requst), 0);

    recv(sockfd, &buf_5, sizeof(buf_5), 0);
    send(sockfd, "a", 2, 0);
    recv(sockfd, &buf_6, sizeof(buf_6), 0);

    if (strcmp(buf_5, "登录成功") == 0)
    {
        if(n==1)
        {
            printf("欢迎钻石会员:%s 登录\n", buf_6);
            return 5;
        }
        else
        {
            printf("欢迎管理员:%s 登录\n",buf_6);
            return 6;
        }
    }
    else
    {
        printf("用户名或密码错误\n");
        return 0;
    }
    return 0;   
}

/*注册模块*/
int sign_in(int sockfd,int n)
{
    char l[2] = {"a"};
    char buf_2[32] = {0};
    requst buf_1={1};
    if(n==1)
    {
        buf_1.who=0;
    }
    else
    {
        buf_1.who=1;
    }

    send(sockfd, l, sizeof(l), 0);

    printf("请输入要注册的ID\n");
    scanf("%d", &buf_1.id);
    printf("请输入要注册的姓名\n");
    scanf("%s", buf_1.name);
    printf("请输入要注册的密码\n");
    scanf("%s", buf_1.pass);
    send(sockfd, &buf_1, sizeof(requst), 0);

    recv(sockfd, &buf_2, sizeof(buf_2), 0);
    if (strcmp(buf_2, "注册成功") == 0)
    {
        printf("注册成功\n");
    }
    return 0;
}
